package com.xmscltd.scms.admin.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.xmscltd.scms.base.common.DepartItem;
import com.xmscltd.scms.base.common.MenuItem;
import com.xmscltd.scms.base.model.Department;
import com.xmscltd.scms.base.model.Res;
import com.xmscltd.scms.base.service.DepartmentService;
import com.xmscltd.scms.base.service.ResService;
import com.xmscltd.scms.base.web.base.BaseController;
import io.jboot.Jboot;
import io.jboot.core.rpc.annotation.JbootrpcService;
import io.jboot.web.controller.annotation.RequestMapping;
import io.swagger.annotations.Api;

import java.util.*;

/**
 * department
 * com.xmscltd.scms.admin.controller.system
 * Created with IntelliJ IDEA.
 * Description:
 * Author: Administrator-cmchen
 * Date: 2018-04-15
 * Time: 17:13
 * Version: V1.0.0
 */
@RequestMapping("/system/department")
@Api(description = "部门管理", basePath = "/system/department", tags = "部门管理接口")
public class DepartController extends BaseController {

    @JbootrpcService
    private DepartmentService departmentService;

    public void index(){

        render("main.html");
    }

    /**
     * 获取部门列表Controller
     */
    public void getDepartmentTree(){
        renderJson( getTreeList(0,null,null));
    }

    /**
     * 获取部门列表
     * @param tid 顶层id
     * @param listmap
     * @param list
     * @return
     */
    private List getTreeList(Integer tid,List<Map<String, Object>> listmap,List<Department> list) {

        //获取所有的部门
        if(listmap == null || list == null ) {
            list = departmentService.findAll();
            listmap = new ArrayList<Map<String,Object>>();
            for(Department d : list) {
                if(d.getParentid() == 0) {
                    //如果上级部门 ==0 ，没有父节点
                    Map<String, Object> map = new HashMap<String, Object>();
                    map.put("id", d.getId().toString());
                    map.put("name", d.getName());
                    map.put("icon",d.getIcon());
                    listmap.add(map);
                }
            }
            getTreeList(tid,listmap,list);

        } else if(listmap.size()>0 && list.size()>0) {
            for(Map<String, Object> mp:listmap) {
                String id = mp.get("id")+"";
                String pid = null;
                List<Map<String, Object>> childlist = new ArrayList<Map<String,Object>>();
                for(Department d : list) {
                    pid = d.getParentid()+"";
                    //遍历子部门放到children
                    if(id.equals(pid)) {
                        Map<String, Object> m = new HashMap<String, Object>();
                        m.put("id", d.getId().toString());
                        m.put("name", d.getName());
                        m.put("icon",d.getIcon());
                        childlist.add(m);
                    }
                }
                //如果存在children  放到map中
                if(childlist.size()>0) {
                    List<String> sizelist = new ArrayList<String>();
                    sizelist.add(childlist.size()+"");
                    mp.put("children", childlist);
                    mp.put("tags", sizelist);
                    getTreeList(tid,childlist,list);
                }
            }
        }
        return listmap;
    }

    /**
     * 获取部门列表Controller
     */
    public void getDepartmentList(){
        List<Department> list = departmentService.findAll();
        renderJson(list);
    }

    /**
     * 部门详情
     */
    public void getDepartmentDetail(){
    	int id = getParaToInt("id");
    	Department depart = departmentService.findById(id);
    	renderJson(depart);
    }
    
}
